package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.MesMachinePaper;
import com.ruoyi.web.domain.vo.InnerAllotVo;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.MesInnerallotDetail;
import com.ruoyi.web.mapper.MesInnerallotInfoMapper;
import com.ruoyi.web.domain.MesInnerallotInfo;
import com.ruoyi.web.service.IMesInnerallotInfoService;

import javax.annotation.Resource;

/**
 * 内部调拨单Service业务层处理
 *
 * @author wxt
 * @date 2024-01-22
 */
@Service
public class MesInnerallotInfoServiceImpl implements IMesInnerallotInfoService {
    @Resource
    private MesInnerallotInfoMapper mesInnerallotInfoMapper;

    @Resource
    private MesPaperService mesPaperService;

    @Resource
    private MesConfigMapper configMapper;

    /**
     * 查询内部调拨单
     *
     * @param id 内部调拨单主键
     * @return 内部调拨单
     */
    @Override
    public MesInnerallotInfo selectMesInnerallotInfoById(Long id) {
        MesInnerallotInfo data = mesInnerallotInfoMapper.selectMesInnerallotInfoById(id);
        return data;
    }

    /**
     * 查询内部调拨单列表
     *
     * @param mesInnerallotInfo 内部调拨单
     * @return 内部调拨单
     */
    @Override
    public List<MesInnerallotInfo> selectMesInnerallotInfoList(MesInnerallotInfo mesInnerallotInfo) {
        return mesInnerallotInfoMapper.selectMesInnerallotInfoList(mesInnerallotInfo);
    }

    @Override
    public List<InnerAllotVo> selectMesDetaillist(MesInnerallotInfo mesInnerallotInfo) {
        List<InnerAllotVo> innerAllotVos = mesInnerallotInfoMapper.selectMesDetaillist(mesInnerallotInfo);
        BigDecimal decimal = mesInnerallotInfoMapper.selectMesDetaillistSumAllotNumber(mesInnerallotInfo);
        if (innerAllotVos.size() > 0) {
            innerAllotVos.get(0).setAllotNumberSum(decimal);
        }
        return innerAllotVos;
    }

    /**
     * 新增内部调拨单
     *
     * @param mesInnerallotInfo 内部调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesInnerallotInfo(MesInnerallotInfo mesInnerallotInfo) {
        mesInnerallotInfo.setCreateTime(DateUtils.getNowDate());

        // 生成调拨单编号
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "innerallot_num");
        map.put("tableName", "mes_innerallot_info");
        int maxNum = configMapper.getMaxId(map);
//        int maxNum = mesInnerallotInfoMapper.getInnerallotInfoMaxId();
        String code = PinYinUtils.getCodeNormal(maxNum, "NBDBD");
        mesInnerallotInfo.setInnerallotNum(code);

        int rows = mesInnerallotInfoMapper.insertMesInnerallotInfo(mesInnerallotInfo);
        insertMesInnerallotDetail(mesInnerallotInfo);
        return rows;
    }

    /**
     * 修改内部调拨单
     *
     * @param mesInnerallotInfo 内部调拨单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesInnerallotInfo(MesInnerallotInfo mesInnerallotInfo) {
        int status = mesInnerallotInfo.getStatus();
        if (status == 2) {//提交
            MesInnerallotInfo info = mesInnerallotInfoMapper.selectMesInnerallotInfoById(mesInnerallotInfo.getId());
            List<MesInnerallotDetail> detaillist = info.getMesInnerallotDetailList();

            if (detaillist.size() != 0) {
                // 获取纸张ids
                List<Long> listId = detaillist.stream().map(it -> it.getPaperid()).collect(Collectors.toList());
                Long[] ids = listId.toArray(new Long[0]);
                MesMachinePaper machinePaper = new MesMachinePaper();
                machinePaper.setPaperids(ids);//纸张id
                machinePaper.setMachineid(info.getOutMachineid());//机台id
                // 根据纸张id和机台查询
                List<MesMachinePaper> MPaper = mesPaperService.selectMachinePaperByIds(machinePaper);

                if (MPaper.size() != 0) {
                    for (MesInnerallotDetail Detail : detaillist) {
                        for (MesMachinePaper mpaper : MPaper) {
                            if (mpaper.getPaperid().equals(Detail.getPaperid())) {
                                BigDecimal total = Detail.getAllotNumber();//调出数量
                                BigDecimal number = mpaper.getNumber();//机台库存量
                                if (number.compareTo(total) < 0) {
                                    throw new ServiceException("调出数量大于机台库存数量！");
                                }
                            }
                        }
                    }
                } else {
                    throw new ServiceException("机台库存没有数据");
                }
                for (MesInnerallotDetail Detail : detaillist) {
                    updateQuantity(info.getOutMachineid(), info.getEnterMachineid(), Detail.getPaperid(), Detail.getAllotNumber());
                }
            }
        } else {
            mesInnerallotInfoMapper.deleteMesInnerallotDetailByInnerallotid(mesInnerallotInfo.getId());
            insertMesInnerallotDetail(mesInnerallotInfo);
        }

        return mesInnerallotInfoMapper.updateMesInnerallotInfo(mesInnerallotInfo);
    }

    /**
     * 批量删除内部调拨单
     *
     * @param ids 需要删除的内部调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesInnerallotInfoByIds(Long[] ids) {
        mesInnerallotInfoMapper.deleteMesInnerallotDetailByInnerallotids(ids);
        return mesInnerallotInfoMapper.deleteMesInnerallotInfoByIds(ids);
    }

    /**
     * 删除内部调拨单信息
     *
     * @param id 内部调拨单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesInnerallotInfoById(Long id) {
        mesInnerallotInfoMapper.deleteMesInnerallotDetailByInnerallotid(id);
        return mesInnerallotInfoMapper.deleteMesInnerallotInfoById(id);
    }

    /**
     * 新增内部调拨单详情信息
     *
     * @param mesInnerallotInfo 内部调拨单对象
     */
    public void insertMesInnerallotDetail(MesInnerallotInfo mesInnerallotInfo) {
        List<MesInnerallotDetail> mesInnerallotDetailList = mesInnerallotInfo.getMesInnerallotDetailList();
        Long id = mesInnerallotInfo.getId();
        if (StringUtils.isNotNull(mesInnerallotDetailList)) {
            List<MesInnerallotDetail> list = new ArrayList<MesInnerallotDetail>();
            for (MesInnerallotDetail mesInnerallotDetail : mesInnerallotDetailList) {
                mesInnerallotDetail.setInnerallotid(id);
                list.add(mesInnerallotDetail);
            }
            if (list.size() > 0) {
                mesInnerallotInfoMapper.batchMesInnerallotDetail(list);
            }
        }
    }

    private void updateQuantity(String cmachineid, String rmachineid, Long paperid, BigDecimal number) {
        MesMachinePaper cmachinepaper = new MesMachinePaper();//调出机台
        cmachinepaper.setMachineid(cmachineid);
        cmachinepaper.setPaperid(paperid);
        cmachinepaper.setNumber(number);
        cmachinepaper.setType((long) 2);
//        int cid= mesPaperService.selectMachinePaper(cmachinepaper);//查询机台纸张
//        if (cid>0){
        mesPaperService.updateMachinePaper(cmachinepaper);//修改机台纸张
//        }else{
//            mesPaperService.AddMachinePaper(cmachinepaper);//添加机台纸张
//        }
        MesMachinePaper rmachinepaper = new MesMachinePaper();//调入机台
        rmachinepaper.setMachineid(rmachineid);
        rmachinepaper.setPaperid(paperid);
        rmachinepaper.setNumber(number);
        rmachinepaper.setType((long) 1);
        int rid = mesPaperService.selectMachinePaper(rmachinepaper);//查询机台纸张
        if (rid > 0) {
            mesPaperService.updateMachinePaper(rmachinepaper);//修改机台纸张
        } else {
            mesPaperService.AddMachinePaper(rmachinepaper);//添加机台纸张
        }
    }
}
